package com.ctrl.riveradministrator.mvp.view.weight;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by lizhunan on 2020/4/8
 */
public class GridSpaceItemDecoration extends RecyclerView.ItemDecoration {

    private int space;
    private int top = 0, bottom = 0, left = 0, right = 0;
    private int num;

    /**
     * 设置Grid间距
     *
     * @param space ITEM间距
     * @param num   列数
     */
    public GridSpaceItemDecoration(int space, int num) {
        this.space = space;
        this.num = num;
    }

    /**
     * 设置Grid间距
     *
     * @param top    Grid和parent的top距离
     * @param bottom Grid和parent的bottom距离
     * @param left   Grid和parent的left距离
     * @param right  Grid和parent的right距离
     * @param space  ITEM间距
     * @param num    列数
     */
    public GridSpaceItemDecoration(int top, int bottom, int left, int right, int space, int num) {
        this.space = space;
        this.top = top;
        this.bottom = bottom;
        this.left = left;
        this.right = right;
        this.num = num;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //不是第一个的格子都设一个左边和底部的间距
        outRect.left = space;
        outRect.top = space;
        if(parent.getChildLayoutPosition(view) == 12||parent.getChildLayoutPosition(view) == 13)
            outRect.bottom = bottom;//如果是最后一行
        if (parent.getChildLayoutPosition(view) % num == 0)
            outRect.left = left; //设置Grid的左边距
        if (parent.getChildLayoutPosition(view) % num == 2)
            outRect.right = right; //设置Grid的右边距
        if (parent.getChildLayoutPosition(view) < num)
            outRect.top = top;//设置Grid的上边距
    }

}
